-- @owner: opentestcase013
-- @date: 2023/7/13
-- @testpoint: 普通ustore表，非压缩表改为压缩表

--step1:建普通表并插入数据;expect:成功
drop table if exists t_alter_rowcompress_0092;
create table t_alter_rowcompress_0092(id int, c1 text) with (storage_type = ustore);
insert into t_alter_rowcompress_0092 select generate_series(1, 1000), 'testrowcompress';

--step2:查看表文件占用磁盘大小;expect:成功
select pg_size_pretty(pg_total_relation_size('t_alter_rowcompress_0092')) as table_size;

--step3:修改表为压缩表，checkpoint;expect:成功
alter table t_alter_rowcompress_0092 set (compresstype = 1);
checkpoint;

--step4:再次查看表文件占用磁盘大小;expect:表文件磁盘占用小于非压缩表
select pg_size_pretty(pg_total_relation_size('t_alter_rowcompress_0092')) as table_size;

--step5:查看压缩率;expect:成功
drop function if exists compress_func_findpath(character varying);
create or replace function compress_func_findpath(character varying)
returns character varying
language plpgsql
as
$body$
declare
relpath character varying;
begin
relpath = (select pg_relation_filepath(relname::regclass) from pg_class where relname = $1);
return relpath;
end;
$body$;
/
select is_compress, file_count, logic_size, physic_size, compress_ratio from pg_catalog.compress_ratio_info(compress_func_findpath('t_alter_rowcompress_0092'));

--step6:查看文件压缩页面的管理信息;expect:成功
select chunk_size, algorithm from pg_catalog.compress_address_header('t_alter_rowcompress_0092', 0);

--step7:查看页面trunk使用的详细信息;expect:成功
select nchunks, chunknos from pg_catalog.compress_address_details('t_alter_rowcompress_0092', 0);

--step8:清理环境;expect:成功
drop function if exists compress_func_findpath(character varying);
drop table if exists t_alter_rowcompress_0092;